#include "loginwindow.h"
#include "ui_loginwindow.h"
#include "sqlhelper.h"
#include <QMessageBox>
#include <QStatusBar>
#include "subpages/checkinmanagementpage.h"
#include "subpages/usermanagemenpage.h"
#include "subpages/checkinpage.h"
#include "usermanager.h"
#include "mainwindow.h"

LoginWindow::LoginWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Login)
    , timer(new QTimer(this))
    , remainingTime(timeout)
{
    ui->setupUi(this);

    timer->setInterval(1000);
    connect(timer, &QTimer::timeout, this, &LoginWindow::updateTimerDisplay);

    connect(ui->lineEditUsername, &QLineEdit::textChanged, this, &LoginWindow::onUserActivityDetected);
    connect(ui->lineEditPassword, &QLineEdit::textChanged, this, &LoginWindow::onUserActivityDetected);


    ui->labelTimer->setText(QString::number(remainingTime) + " 秒");
    timer->start();
}

void LoginWindow::onTimeout()
{
    timer->stop();
    ui->labelTimer->setText("超时！");
    QMessageBox::information(this, "超时退出", "由于长时间未操作，系统已自动退出！");
    this->close();
}

void LoginWindow::updateTimerDisplay()
{
    remainingTime--;
    ui->labelTimer->setText(QString::number(remainingTime) + " 秒");
    if (remainingTime <= 0) {
        onTimeout(); // 调用超时处理槽函数
    }
}

void LoginWindow::onUserActivityDetected()
{
    timer->stop();
    remainingTime = timeout;
    ui->labelTimer->setText(QString::number(remainingTime) + " 秒");
    timer->start();
}

LoginWindow::~LoginWindow()
{
    delete timer;
    delete ui;
}

void LoginWindow::on_pushButtonLogin_clicked()
{
    timer->stop();

    QString username = ui->lineEditUsername->text();
    QString password = ui->lineEditPassword->text();


    SqlHelper& sqlHelper = SqlHelper::getInstance();
    UserManager& userManager = UserManager::getInstance();

    auto result = sqlHelper.AuthLogin(username, password);

    if (result) {
        QVariantList userInfo = *result;
        QString userInfoRole = userInfo.value(2).toString();

        userManager.setUserInfo(username, password, [&]() ->UserManager::Role {
                if (userInfoRole == "系统管理员") {
                    MainWindow::getMainWindow()->statusBar()->showMessage("欢迎系统管理员：" + username + "，祝您使用愉快！", 20 * 1000);
                    return UserManager::SystemAdmin;
                }
                else if (userInfoRole == "业务管理员") {
                    MainWindow::getMainWindow()->statusBar()->showMessage("欢迎业务管理员：" + username + "，祝您使用愉快！", 20 * 1000);
                    return UserManager::BusinessAdmin;
                }
                else if (userInfoRole == "用户") {
                    MainWindow::getMainWindow()->statusBar()->showMessage("欢迎用户：" + username + "，祝您使用愉快！", 20 * 1000);
                    return UserManager::User;
                }
                else {
                    return UserManager::Unlogin;
                }
            }()
        );

        emit loginSucc();
        MainWindow::getMainWindow()->setupMenus();
        this->close();
    }
    else {
        QMessageBox::warning(this, "登录失败", "用户名、密码或角色错误！");
    }
}

void LoginWindow::on_pushButtonExit_clicked()
{
    timer->stop();
    close();
}


